package com.test.controller;

import com.test.service.ProductsService;
import com.test.po.Products;
import com.test.po.ProductsExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.List;
@SessionAttributes({"pid","products"})
@Controller
public class ProductController {


    @Autowired

    private ProductsService productsService;

    @RequestMapping("/toIndex.do")

    public String selectDate(ModelMap map, String searchword){
        ProductsExample productsExample = new ProductsExample();
        if(searchword != null && !"".equals(searchword.trim())){
            productsExample.createCriteria().andPnameLike("%"+searchword+"%");
        }

        List<Products> productsList = productsService.selectByExample(productsExample);

        map.addAttribute("productsList",productsList);

        return "index";

    }
    @RequestMapping("/padd.do")

    public String insertproduct(Products products){

        productsService.insert(products);

        return "forward:forplist.do";

    }
    @RequestMapping("/pmodify.do")

    public String product(Integer pid,ModelMap map){
        map.addAttribute("pid",pid);
        map.addAttribute("products",productsService.selectByPrimaryKey(pid));
        System.out.println(pid);
        return "pmodify";

    }
    @RequestMapping("/pmodify1.do")

    public String updateproduct(Products products,ModelMap map){
        productsService.updateByPrimaryKeySelective(products);
        return "forward:forplist.do";

    }
    /*@RequestMapping("/pdelete.do")
    public String udelete(Integer pid){
        productsService.deleteByPrimaryKey(pid);
        return "forward:forplist.do";
    }*/

    @RequestMapping("/pdelete.do")
    public String udelete(Integer pid){
        productsService.updateById(pid);
        return "forward:forplist.do";
    }



}
